package edu.geo.algorithm.graph;

import java.util.ArrayList;
import java.util.List;

import com.vividsolutions.jts.geom.Point;

import edu.geo.containers.geom.impl.LineSegment;

public class Graph 
{
	private List<LineSegment> lines;
	public List<Arc> getArcs() {
		return arcs;
	}

	public void setArcs(List<Arc> arcs) {
		this.arcs = arcs;
	}

	private List<Arc> arcs;
	
	public Graph(List<LineSegment> lines)
	{
		arcs=new ArrayList<Arc>();
		this.lines=lines;
		for(LineSegment line:lines)
		{
			arcs.add(new Arc(line.getId(),line));
		}
	}
	

	
	public List<LineSegment> getLine() {
		return lines;
	}
	
	public void buildGraph()
	{
		int arcsSice =arcs.size();
		for(int i=0;i<arcsSice;i++)
		{
			LineSegment line=arcs.get(i).getGeom();
			Point sp= line.getStartPoint();
			Point ep= line.getEndPoint();
			for(int j=0;j<arcsSice;j++)
			{
				if(i!=j)
				{
					LineSegment lineComp=arcs.get(j).getGeom();
					Point cSp =lineComp.getStartPoint();
					Point cEp =lineComp.getEndPoint();
					if(sp.equals(cSp)||sp.equals(cEp))
					{
						arcs.get(i).AddPredeccessor(arcs.get(j));
					}
					if(ep.equals(cSp)||ep.equals(cEp))
					{
						arcs.get(i).AddSuccessor(arcs.get(j));
					}
				}
			}
		}
		
	}
	
 public boolean haveLeafs()
  {
	  for(Arc arc:arcs)
	  {
		  if( arc.isLeaf())
		  {
			  return true;  
		  }
		  
	  }
	  return false;  
  }
  
  
  public Graph cutLeafs()
  {
	  ArrayList<LineSegment> lineList= new ArrayList<LineSegment>();  
	for(Arc ac:arcs)
	{
		if(!ac.isLeaf())
		{
			lineList.add(ac.getGeom());	
		}
	}	  
	Graph resultGraph =  new Graph(lineList); 
	resultGraph.buildGraph();
	return resultGraph;  
  }
}
